package ca.ualberta.cs.CLRDB.conference;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

import ca.ualberta.cs.CLRDB.DatabaseController;
import ca.ualberta.cs.CLRDB.FriendsManagement;
import ca.ualberta.cs.CLRDB.PersonsManagement;
import ca.ualberta.cs.CLRDB.User;
import ca.ualberta.cs.CLRDB.interfaces.EntityRelManagement;
import ca.ualberta.cs.CLRDB.interfaces.PersonAccess;
import ca.ualberta.cs.DB2ExceptionHandling.DB2SqlException;

public class PersonsMgmtConference extends EntityRelManagement implements
		PersonAccess {

	private static PersonsManagement personsMgmt = new PersonsManagement();
	
	@Override
	public void initialize(Statement statement) throws SQLException {		
		personsMgmt.initialize(statement);
	}

	public int addAuthor(User user) {
		// TODO Auto-generated method stub
		return 0;
	}

	public int addUser(User user) {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	    * @param username
	    * @param pass
	    * @return HashMap<String,String> where key: state, values is: 0: ok, 1: wrong pass, 2:no such user, 3: exception, 4: wrong/missing role
	    */
	public HashMap<String, String> authenticate(String username, String pass) {
		
		HashMap<String, String> state = new HashMap<String, String>();
		if(role == null){
			state.put("state", "4");
			return state;
		}
		
		Connection con = DatabaseController.connectToDB();
   	   	Statement statement;
		try {
			statement = con.createStatement();
			
			//check if this user has the specified role:
			boolean hasRole = RolesManagement.userHasRole(username, statement);
			if( !hasRole ){
				state.put("state", "4");
				return state;				
			}
			
			return personsMgmt.authenticate(username, pass);
			
		}catch(SQLException e){
			DB2SqlException.printSqlErrorMsg(e);
			state.put("state", "3");
			return state;
		}
			
	}

	public int getPersonId(String username, Connection con) {
		// TODO Auto-generated method stub
		return 0;
	}

	public FriendsManagement getFriendsManagement() {
		// TODO Auto-generated method stub
		return null;
	}

}
